import { Component,OnDestroy,Input,Output,EventEmitter } from '@angular/core';
import { Dialog } from './dialog';
import { MdIcon, MD_ICON_DIRECTIVES } from "@angular2-material/icon/icon";

export class LoadMaskService{
    private loadMsk: LoadMask;
    static Current = new LoadMaskService();
    Regist(loadMsk: LoadMask){
        this.loadMsk = loadMsk;
    }
    show(){
        if(this.loadMsk){
            this.loadMsk.loading = true;
        }
    }
    hide(){
        if(this.loadMsk){
            this.loadMsk.loading = false;
        }
    }
}

@Component({
    selector: 'load-mask',
    template: `
        <div *ngIf="loading" class="fix-all">
            <div class="divtcvma" style="text-align:center; color: black;">
                <i  class=" icon-loading iconfont icon-refresh "></i>
                <p style="padding:30px;margin:0;font-size: 16px;">加载中</p>
            </div>
        </div>
    `
})
export class LoadMask{
    constructor(){
        LoadMaskService.Current.Regist(this);
    }
    loading: boolean =  false;
}


export function loadingMask() {
    return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
        let method = descriptor.value;
        descriptor.value = function () {
            LoadMaskService.Current.show();
            try {
                let that = this;
                let args = arguments;
                var promise = new Promise(function(resolve, reject){
                    resolve(method.apply(that, args));
                });
                promise.then(function(){
                    LoadMaskService.Current.hide();
                })
            } catch (error) {

            } finally {
                
            }
        }
    }
}


